package com.itheima.health.service.impl;

import com.itheima.health.dao.MemberDao;
import com.itheima.health.dao.OrderDao;
import com.itheima.health.dao.SetMealDao;
import com.itheima.health.pojo.HotSetmeal;
import com.itheima.health.service.ReportService;
import com.itheima.health.service.SetMealService;
import com.itheima.health.vo.ReportVo;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

/**
 * PROJECT_NAME: heima-health
 * NAME: ReportServiceImpl
 * AUTHOR: lyz
 * DATE: 2023/9/3
 * DESCRIPTION : TODO
 */
@Service
public class ReportServiceImpl implements ReportService {
    @Resource
    private MemberDao memberDao;
    @Resource
    private OrderDao orderDao;
    @Resource
    private SetMealDao setMealDao;

    /**
     * 数据统计报表导出
     */
    @Override
    public void exportExcel(HttpServletResponse response) {
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        LocalDate now = LocalDate.now();
        // 本周第一天
        LocalDate monday = now.with(DayOfWeek.MONDAY);
        // 本月第一天
        LocalDate firstDayofMonth = now.withDayOfMonth(1);
        InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("template/运营统计模板.xlsx");
        try {
            XSSFWorkbook excel = new XSSFWorkbook(inputStream);
            XSSFSheet sheet = excel.getSheet("Sheet1");
            sheet.getRow(1).getCell(2).setCellValue(dateTimeFormatter.format(now));
            sheet.getRow(3).getCell(2).setCellValue(memberDao.getMemberCountByDate(now));
            sheet.getRow(3).getCell(4).setCellValue(memberDao.getMemberTotalCount());
            sheet.getRow(4).getCell(2).setCellValue(memberDao.getMemberCountAfterDate(monday));
            sheet.getRow(4).getCell(4).setCellValue(memberDao.getMemberCountAfterDate(firstDayofMonth));
            sheet.getRow(6).getCell(2).setCellValue(orderDao.getOrderCountByDate(now));
            sheet.getRow(6).getCell(4).setCellValue(orderDao.getVisitsCountByDate(now));
            sheet.getRow(7).getCell(2).setCellValue(orderDao.getOrderCountAfterDate(monday));
            sheet.getRow(7).getCell(4).setCellValue(orderDao.getVisitsCountAfterDate(monday));
            sheet.getRow(8).getCell(2).setCellValue(orderDao.getOrderCountAfterDate(firstDayofMonth));
            sheet.getRow(8).getCell(4).setCellValue(orderDao.getVisitsCountAfterDate(firstDayofMonth));
            List<HotSetmeal> hotSetmeal = orderDao.getHotSetmeal();
            for (int i = 0; i < hotSetmeal.size(); i++) {
                XSSFRow row = sheet.getRow(11 + i);
                row.getCell(1).setCellValue(hotSetmeal.get(i).getName());
                row.getCell(2).setCellValue(hotSetmeal.get(i).getSetmeal_count());
                row.getCell(3).setCellValue(hotSetmeal.get(i).getProportion());
            }
                response.setContentType("application/vnd.ms-excel");
                response.setHeader("content-Disposition","attachment;filename=report.xlsx");
                ServletOutputStream outputStream = response.getOutputStream();
                excel.write(outputStream);
                outputStream.flush();
                outputStream.close();
                excel.close();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 运营数据统计
     */
    @Override
    public ReportVo getBusinessReportData() {
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        LocalDate now = LocalDate.now();
        // 本周第一天
        LocalDate monday = now.with(DayOfWeek.MONDAY);
        // 本月第一天
        LocalDate firstDayofMonth = now.withDayOfMonth(1);
        String reportDate = dateTimeFormatter.format(now);
        // 今日新增会员数
        Integer todayNewMember = memberDao.getMemberCountByDate(now);
        // 总会员数
        Integer totalMember = memberDao.getMemberTotalCount();
        // 本周新会员数
        Integer thisWeekNewMember = memberDao.getMemberCountAfterDate(monday);
        // 本月新会员数
        Integer thisMonthNewMember = memberDao.getMemberCountAfterDate(firstDayofMonth);
        // 今日预约人数
        Integer todayOrderNumber = orderDao.getOrderCountByDate(now);
        // 本周预约人数
        Integer thisWeekOrderNumber = orderDao.getOrderCountAfterDate(monday);
        // 本月预约人数
        Integer thisMonthOrderNumber = orderDao.getOrderCountAfterDate(firstDayofMonth);
        // 今日到诊人数
        Integer todayVisitsNumber = orderDao.getVisitsCountByDate(now);
        // 本周到诊人数
        Integer thisWeekVisitsNumber = orderDao.getVisitsCountAfterDate(monday);
        // 本月到诊人数
        Integer thisMonthVisitsNumber = orderDao.getVisitsCountAfterDate(firstDayofMonth);
        // 热门套餐
        List<HotSetmeal> hotSetmeals = orderDao.getHotSetmeal();

        return ReportVo.builder().reportDate(reportDate).todayNewMember(todayNewMember).thisMonthNewMember(thisMonthNewMember)
                .thisWeekNewMember(thisWeekNewMember).totalMember(totalMember).todayOrderNumber(todayOrderNumber)
                .thisWeekOrderNumber(thisWeekOrderNumber).thisMonthOrderNumber(thisMonthOrderNumber)
                .todayVisitsNumber(todayVisitsNumber).thisWeekVisitsNumber(thisWeekVisitsNumber)
                .thisMonthVisitsNumber(thisMonthVisitsNumber).hotSetmeal(hotSetmeals).build();
    }
}
